{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fb650904",
   "metadata": {},
   "source": [
    "* 2023-10-23\n",
    "* 课程：Python语言\n",
    "* week08课堂"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "04b46415",
   "metadata": {},
   "source": [
    "# 1.列表进阶\n",
    "# 2.课本项目\n",
    "# 3.购物车项目"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1db43c31",
   "metadata": {},
   "source": [
    "## 1.1 接上周内容（） 列表的方法--remove"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "64cff0c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex']"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"张三\",\"李四\",\"小明\",\"Jalex\"]\n",
    "\n",
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "177431eb",
   "metadata": {},
   "source": [
    "* 思考：我怎么知道‘李鹏’在数据列表里面呢？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f96d29b5",
   "metadata": {},
   "source": [
    "> 1.先查询   \n",
    "> 2.后删除"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e0161601",
   "metadata": {},
   "source": [
    "* in 方法(查询元素的方法)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "8808c2ac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三\n"
     ]
    }
   ],
   "source": [
    "for i in name_list:\n",
    "    if i == \"张三\":\n",
    "        print(i)\n",
    "        name_list.remove(\"张三\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "69c0ea19",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李四', '小明', 'Jalex']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "76833f34",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"小明\" in name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "f74833dc",
   "metadata": {},
   "outputs": [],
   "source": [
    "if \"小明\" in name_list:\n",
    "    name_list.remove(\"小明\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6ad0a737",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李四', 'Jalex']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7eb3112f",
   "metadata": {},
   "source": [
    "## 1.2 列表的其他方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fe97aacf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['李四', 'Jalex']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# clear 清空 ，清除列表中的所有元素\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f4dd8353",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f00dbb88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e4058c88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# count 查看list中统计元素的数量\n",
    "name_list = [\"张三\",\"李四\",\"小明\",\"Jalex\",\"李四\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "109fd1ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list.count(\"李四\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4c7e129f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 案例\n",
    "# 假设掷骰子100次，统计1-6各出现多少次\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "c53ebcbd",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 是不是需要现有一个空列表？来存放我们产生的数据呢？\n",
    "random_list = []\n",
    "# 增加数据到列表里面？\n",
    "for i in range(100):\n",
    "    random_list.append(random.randint(1,6))\n",
    "#     print(random.randint(1,6))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "71e9de67",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2, 2, 1, 3, 5, 3, 6, 2, 4, 4, 1, 6, 6, 3, 1, 1, 6, 1, 4, 2, 4, 1, 6, 1, 4, 3, 3, 5, 4, 5, 5, 4, 2, 4, 3, 3, 1, 3, 2, 3, 4, 2, 5, 5, 3, 5, 3, 4, 3, 4, 5, 1, 1, 3, 4, 1, 4, 4, 6, 5, 6, 2, 2, 6, 4, 2, 4, 1, 2, 2, 2, 3, 3, 4, 5, 2, 3, 1, 1, 2, 3, 6, 6, 6, 5, 3, 3, 2, 1, 6, 3, 4, 2, 1, 4, 5, 1, 6, 2, 5]\n"
     ]
    }
   ],
   "source": [
    "print(random_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "cb23f14b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 17 0.17\n",
      "2 18 0.18\n",
      "3 20 0.2\n",
      "4 19 0.19\n",
      "5 13 0.13\n",
      "6 13 0.13\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,7):\n",
    "    print(i,random_list.count(i),random_list.count(i)/100)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "49e604fd",
   "metadata": {},
   "source": [
    "## 1.3 列表元素的排序 sort() 和reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2eb6315d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list = [\"张三\",\"李四\",\"小明\",\"Jalex\",\"李四\"]\n",
    "name_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "419c7999",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "6ce440bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Jalex', '小明', '张三', '李四', '李四']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list\n",
    "# 排序，例如：0，1，2，3.../"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f3afb513",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 18, 19, 21, 22, 30, 17]"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list = [20,18,19,21,22,30,17]\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c8cf74d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[17, 18, 19, 20, 21, 22, 30]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list.sort()\n",
    "age_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "837f1f96",
   "metadata": {},
   "outputs": [],
   "source": [
    "age_list.reverse()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "d19ba5a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[30, 22, 21, 20, 19, 18, 17]"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "age_list\n",
    "# 倒序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6c03404",
   "metadata": {},
   "source": [
    "## 1.4 copy()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cfd91419",
   "metadata": {},
   "source": [
    "> =   \n",
    "> copy()   \n",
    "> deep copy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "84af705b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2928250157504\n",
      "2928250437776\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 通过id可以看出变化，是“真”复制还是“假”复制。\n",
    "name_list01 = [\"张三\",\"李四\",\"小明\",\"Jalex\",\"李四\"]\n",
    "print(id(name_list01))\n",
    "print(id(name_list01[0]))\n",
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "0d868acc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2928250157504\n",
      "2928250437776\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02 = name_list01\n",
    "print(id(name_list02))\n",
    "print(id(name_list02[0]))\n",
    "name_list02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8cd81149",
   "metadata": {},
   "outputs": [],
   "source": [
    "name_list01.append('Marry')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "f4207ddb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四', 'Marry']"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "b3771ae6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四', 'Marry']"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea928af3",
   "metadata": {},
   "source": [
    "* 如上原因：我们不得不找一个方法来实现“真正的”复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "6f3c919d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2928250222656\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01 = [\"张三\",\"李四\",\"小明\",\"Jalex\",\"李四\"]\n",
    "print(id(name_list01))\n",
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "581c76ca",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2928250359296\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02 = name_list01.copy()\n",
    "print(id(name_list02))\n",
    "name_list02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "68d1adea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'李四'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01.pop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "4238281e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "25060fcd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['张三', '李四', '小明', 'Jalex', '李四']"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name_list02"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd88dffd",
   "metadata": {},
   "source": [
    "# 2. 课后实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9179b73a",
   "metadata": {},
   "source": [
    "## 2.1 实践：购物车项目"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ce8b824",
   "metadata": {},
   "source": [
    "购物车程序项目：\n",
    "\n",
    "> 运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。    \n",
    "> 让用户输入商品编号进行商品的购买。    \n",
    "> 用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。    \n",
    "> 用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6321e867",
   "metadata": {},
   "source": [
    "# 1. \n",
    "Alipay = input()\n",
    "商品列表 = [\n",
    "    \"内容\"\n",
    "]\n",
    "\n",
    "print(商品列表)\n",
    "购物车列表 = [\n",
    "\n",
    "]\n",
    "\n",
    "# 2. # 3.\n",
    "users_choese = input()\n",
    "if 钱包余额 > 商品价格:\n",
    "    扣钱，加商品进购物车列表（append）\n",
    "else\n",
    "    print(\"你的钱不够，......\")    \n",
    "\n",
    "# 4. \n",
    "break 退出循环，打印购物车列表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b72d1d6c",
   "metadata": {},
   "source": [
    "## 2.2 实践：课本第二章列表的项目"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4f6876f8",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
